/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.apt.matrimony.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apt.matrimony.methods.login;
import org.apt.matrimony.utils.Tools;

/**
 *
 * @author yoko
 */
public class doLogin extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         try {
           String user=request.getParameter("username");
            String pass=request.getParameter("password");
            if(user.equals("")){
                request.setAttribute("Invalid_input","User input must not be empty");
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
            if(pass.equals("")){
                request.setAttribute("Invalid_input","Password input must not be empty");
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
            /** 2 login as member **/
            if(login.checkLogin(user, pass)==2){
                if(Tools.checkDate(user)){
                    request.getRequestDispatcher("expiredmsg.jsp").forward(request, response);
                }
                if(login.checkFirstTimeLogin(user)){
                    request.getSession().setAttribute("ActiveUser", user);
                    request.getRequestDispatcher("completprofile.jsp?username="+user).forward(request, response);
                }
                login.updateLoginCount(user);
                request.getSession().setAttribute("ActiveUser", user);
                request.getRequestDispatcher("index_member.jsp?username="+user).forward(request, response);
            }
            /** 1 login as admin **/
            if(login.checkLogin(user, pass)==1){
                request.getSession().setAttribute("ActiveAdmin", user);
                request.getRequestDispatcher("index_admin.jsp").forward(request, response);
            }
            /** 3 password or username is not correct **/
            if(login.checkLogin(user, pass)==0){
                //khi login thanh cong thi no se truyen cai nguoi dung do di
//                cung voi dong String la Invalidate Acfcount sang trang login.jsp
//                rôi ạ
                request.setAttribute("Invalid_input", "Invalid Account");
                request.getRequestDispatcher("login.jsp?login_fail=1").forward(request, response);
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
